CMAKE_MINIMUM_REQUIRED(VERSION 2.6)

#project name
PROJECT(SHLAPI)

#introot definition
SET(INTROOT $ENV{INTROOT})
IF(NOT INTROOT)
   SET(INTROOT $ENV{SWROOT})
ENDIF(NOT INTROOT)

IF(NOT INTROOT)
    SET(CMAKE_INSTALL_PREFIX="/ari/SWROOT")
    MESSAGE("INTROOT not defined, CMAKE_INSTALL_PREFIX set to:/ari/SWROOT")
ELSE (NOT INTROOT)
    SET(CMAKE_INSTALL_PREFIX ${INTROOT})
    MESSAGE("CMAKE_INSTALL_PREFIX set to: $ENV{INTROOT}")
ENDIF (NOT INTROOT)

INCLUDE_DIRECTORIES(/usr/include/python2.7)
INCLUDE_DIRECTORIES("${PROJECT_SOURCE_DIR}/")
INCLUDE_DIRECTORIES("${PROJECT_SOURCE_DIR}/../include")
INCLUDE_DIRECTORIES("${INTROOT}/include")

#for ../lib/libCUSBSA.so 
ADD_LIBRARY(CUSBSA SHARED CUSBSA.cpp)
TARGET_LINK_LIBRARIES(CUSBSA shapi ftd2xx pthread dl rt)
set_property(TARGET CUSBSA PROPERTY LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../lib)

#for ../bin/testSH executable 
LINK_DIRECTORIES(${INTROOT}/lib)
ADD_EXECUTABLE(testSH main.cpp)
TARGET_LINK_LIBRARIES(testSH CUSBSA shapi ftd2xx pthread dl rt)
set_property(TARGET testSH PROPERTY RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../bin)

#generate swig wrapper files
ADD_CUSTOM_COMMAND(
    OUTPUT ${PROJECT_SOURCE_DIR}/SHLAPI_wrap.cxx
    DEPENDS ${PROJECT_SOURCE_DIR}/SHLAPI.i 
    COMMAND ${CMAKE_COMMAND} -E echo " SHLAPI_wrap.cxx generated."
    COMMAND swig -python -c++ ${PROJECT_SOURCE_DIR}/SHLAPI.i
    COMMENT "generating swig wrapper files..." 
)

ADD_LIBRARY(_SHLAPI SHARED SHLAPI_wrap.cxx )
TARGET_LINK_LIBRARIES(_SHLAPI shapi ftd2xx pthread)
set_property(TARGET _SHLAPI PROPERTY LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../lib)
set_target_properties(_SHLAPI PROPERTIES PREFIX "")

#generate swig wrapper files for CUSBSA_wrap.cxx
ADD_CUSTOM_COMMAND(
    OUTPUT ${PROJECT_SOURCE_DIR}/CUSBSA_wrap.cxx
    DEPENDS ${PROJECT_SOURCE_DIR}/CUSBSA.i 
    COMMAND ${CMAKE_COMMAND} -E echo " CUSBSA_wrap.cxx generated."
    COMMAND swig -python -c++ ${PROJECT_SOURCE_DIR}/CUSBSA.i
    COMMENT "generating swig wrapper files..." 
)

ADD_LIBRARY(_CUSBSA SHARED CUSBSA_wrap.cxx )
TARGET_LINK_LIBRARIES(_CUSBSA shapi ftd2xx pthread)
set_property(TARGET _CUSBSA PROPERTY LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../lib)
set_target_properties(_CUSBSA PROPERTIES PREFIX "")


#delete generated files in make clean 
SET_DIRECTORY_PROPERTIES (
   PROPERTIES 
     ADDITIONAL_MAKE_CLEAN_FILES "LCU.h"
     ADDITIONAL_MAKE_CLEAN_FILES "LCU.cpp"
     ADDITIONAL_MAKE_CLEAN_FILES "${PROJECT_SOURCE_DIR}/generated"
)

#for make install
INSTALL(TARGETS CUSBSA
    RUNTIME DESTINATION bin
    LIBRARY DESTINATION lib
    ARCHIVE DESTINATION lib
)

#install *.ice files into $INTROOT/idl
INSTALL(
   FILES ${PROJECT_SOURCE_DIR}/../idl/LCU.ice DESTINATION idl
)

#install generated *.h files into $INTROOT/includes
INSTALL(
   FILES ${PROJECT_SOURCE_DIR}/LCU.h DESTINATION include 
)

#install configuration files into $INTROOT/config
INSTALL(
   FILES ${PROJECT_SOURCE_DIR}/../config/LCU-config 
         ${PROJECT_SOURCE_DIR}/../config/ESO50cm.conf DESTINATION config 
)

#install .h 
INSTALL(
   DIRECTORY ${PROJECT_SOURCE_DIR}/../include/ DESTINATION include 
   FILES_MATCHING PATTERN "*.h"
)

#install python libraries
IF(NOT LCU)
   INSTALL(
      DIRECTORY "${PROJECT_SOURCE_DIR}/generated/" DESTINATION lib/python/site-packages 
      FILES_MATCHING PATTERN "*.py"
   )
   INSTALL(
      DIRECTORY ${PROJECT_SOURCE_DIR}/generated/OUC DESTINATION lib/python/site-packages
   )
   INSTALL(
      FILES ${PROJECT_SOURCE_DIR}/AstroUtil.py DESTINATION lib/python/site-packages
   )
   INSTALL(
      FILES ${PROJECT_SOURCE_DIR}/DeviceManager.py DESTINATION lib/python/site-packages
   )
   INSTALL(
      FILES ${PROJECT_SOURCE_DIR}/Client-lcu.py DESTINATION bin
   )
   INSTALL(
      FILES ${PROJECT_SOURCE_DIR}/get_position.py DESTINATION bin
      PERMISSIONS OWNER_READ GROUP_READ WORLD_READ OWNER_EXECUTE GROUP_EXECUTE WORLD_EXECUTE
   )
   INSTALL(
      FILES ${PROJECT_SOURCE_DIR}/offset_telescope.py DESTINATION bin
      PERMISSIONS OWNER_READ GROUP_READ WORLD_READ OWNER_EXECUTE GROUP_EXECUTE WORLD_EXECUTE
   )
   INSTALL(
      FILES ${PROJECT_SOURCE_DIR}/read_encoders.py DESTINATION bin
      PERMISSIONS OWNER_READ GROUP_READ WORLD_READ OWNER_EXECUTE GROUP_EXECUTE WORLD_EXECUTE
   )
   INSTALL(
      FILES ${PROJECT_SOURCE_DIR}/read_motors_encoders.py DESTINATION bin
      PERMISSIONS OWNER_READ GROUP_READ WORLD_READ OWNER_EXECUTE GROUP_EXECUTE WORLD_EXECUTE
   )
   INSTALL(
      FILES ${PROJECT_SOURCE_DIR}/set_tracking.py DESTINATION bin
      PERMISSIONS OWNER_READ GROUP_READ WORLD_READ OWNER_EXECUTE GROUP_EXECUTE WORLD_EXECUTE
   )
   INSTALL(
      FILES ${PROJECT_SOURCE_DIR}/stop_telescope.py DESTINATION bin
      PERMISSIONS OWNER_READ GROUP_READ WORLD_READ OWNER_EXECUTE GROUP_EXECUTE WORLD_EXECUTE
   )
   INSTALL(
      FILES ${PROJECT_SOURCE_DIR}/Client-position.py DESTINATION bin
   )
ENDIF(NOT LCU)
